package com.testclone;

import java.util.ArrayList;
import java.util.LinkedList;

/**
 * Created by yijy on 2018/3/2.
 * 栈(先进后出)
 * 1,用ArrayList实现栈，实现push和pop功能
 * 2.用LinkedList实现栈
 */
public class MyStack<E> {

    public MyStack() {
    }

    private ArrayList list;

    public synchronized void push(E e){
        if(list == null){
            list = new ArrayList();
        }
        list.add(list.size(),e);
    }

    public synchronized E pop(){
        if(list!=null){
            E e = (E) list.get(list.size()-1);
            list.remove(list.size()-1);
            return e;
        }
        throw new RuntimeException("The Stack is null!");
    }

    public boolean isEmpty(){
        return list.isEmpty();
    }

    private LinkedList linked;

    public synchronized void pushlinked(E e){
        if(linked == null){
            linked = new LinkedList();
        }
        linked.add(e);
    }

    public synchronized E poplinked(){
        if(linked!=null){
            E e = (E) linked.removeLast();
            return e;
        }
        throw new RuntimeException("The Stack is null!");
    }

    public boolean isEmpty1(){
        return linked.isEmpty();
    }
}
